package com.example.nfc.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.nfc.R;
import com.example.nfc.server.response.Rows;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Create by 游德禄 on 2019-9-21
 * 已采集列表 开机密码
 */
public class GgjsyztAdapter extends RecyclerView.Adapter<GgjsyztAdapter.RecyclerViewHolder>{
    private LayoutInflater inflater;
    private List<Rows> data = new ArrayList<>();
    private ClickCallBack callBack;


    public GgjsyztAdapter(Context context, ClickCallBack clickCallBack) {
        this.inflater= LayoutInflater.from(context);
        this.callBack = clickCallBack;
    }


    /**
     *
     * @param data
     */
    public void update(List<Rows> data){
        this.data.clear();
        this.data = new ArrayList<>(data);
        this.notifyDataSetChanged();
    }
    public void updateById(String id,String zt){
        boolean isUpdate = false;
        for(Rows rows:data){
            if(id.equals(rows.getINFO_ID())){
                rows.setINFO_ZT(zt);
                isUpdate = true;
                break;
            }
        }
        if(isUpdate) {
            this.notifyDataSetChanged();
        }
    }



    public int getSize(){
        return this.data.size();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public Rows getItem(int i) {
        if(data.size()>0)
            return data.get(i);
        else
            return null;
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        final Rows data = getItem(position);
        holder.btn1.setTag(position);
        holder.btn2.setTag(position);
        holder.btn3.setTag(position);
        holder.btn4.setTag(position);
        holder.btn5.setTag(position);
        if(data.isSelect()){
            holder.box.setBackgroundResource(R.drawable.collect_type_select_sharp);
        }else{
            holder.box.setBackgroundResource(R.drawable.collect_type_sharp);
        }
        if(data!=null) {
            holder.name.setText(data.getINFO_MC());
            if(!TextUtils.isEmpty(data.getINFO_ZT())){
                switch (data.getINFO_ZT()){
                    case "xz":
                        holder.btn1.setBackgroundResource(R.drawable.state1_btn_sharp);
                        holder.btn2.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn3.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn4.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn5.setBackgroundResource(R.drawable.state0_btn_sharp);
                        break;
                    case "sy":
                        holder.btn1.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn2.setBackgroundResource(R.drawable.state2_btn_sharp);
                        holder.btn3.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn4.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn5.setBackgroundResource(R.drawable.state0_btn_sharp);
                        break;
                    case "sh":
                        holder.btn1.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn2.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn3.setBackgroundResource(R.drawable.state3_btn_sharp);
                        holder.btn4.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn5.setBackgroundResource(R.drawable.state0_btn_sharp);
                        break;
                    case "djc":
                        holder.btn1.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn2.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn3.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn4.setBackgroundResource(R.drawable.state4_btn_sharp);
                        holder.btn5.setBackgroundResource(R.drawable.state0_btn_sharp);
                        break;
                    case "yc":
                        holder.btn1.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn2.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn3.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn4.setBackgroundResource(R.drawable.state0_btn_sharp);
                        holder.btn5.setBackgroundResource(R.drawable.state5_btn_sharp);
                        break;

                }
            }
        }
    }

    @Override
    @NonNull
    public GgjsyztAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.ggjsyzt_item, parent, false);
        return new GgjsyztAdapter.RecyclerViewHolder(view);
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.box)
        LinearLayout box;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.btn1)
        Button btn1;
        @Bind(R.id.btn2)
        Button btn2;
        @Bind(R.id.btn3)
        Button btn3;
        @Bind(R.id.btn4)
        Button btn4;
        @Bind(R.id.btn5)
        Button btn5;
        RecyclerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this,view);
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.btn1(position);
                    }
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.btn2(position);
                    }
                }
            });
            btn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.btn3(position);
                    }
                }
            });
            btn4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.btn4(position);
                    }
                }
            });
            btn5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object obj = v.getTag();
                    int position = (int)obj;
                    if(callBack!=null){
                        callBack.btn5(position);
                    }
                }
            });
        }
    }

    public int findPositionById(String id){
        int i = 0 ;
        for(Rows o:data){
            if(id.equals(o.getINFO_FRID())){
                break;
            }
            i++;
        }
        return i;
    }
    public void setSelected(int position){
        if(position<data.size()) {
            this.data.get(position).setSelect(true);
            notifyDataSetChanged();
        }
    }

    public void clearSelect(){
        for(Rows rows:data){
            rows.setSelect(false);
        }
        notifyDataSetChanged();
    }

    public interface ClickCallBack {
         void btn1(int position);
        void btn2(int position);
        void btn3(int position);
        void btn4(int position);
        void btn5(int position);
    }
}
